package com.edgclub.edg.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.activity.ChatActivity;
import com.edgclub.edg.adapter.adapteruitls.GzBaseAdapter;
import com.edgclub.edg.adapter.adapteruitls.MyItemClickListener;
import com.edgclub.edg.adapter.adapteruitls.MyItemLongClickListener;
import com.edgclub.edg.adapter.adapteruitls.RViewHolder;
import com.edgclub.edg.api.ApiConfig;
import com.edgclub.edg.api.FriendApi;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.param.MessageParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.GroupBean;
import com.edgclub.edg.model.result.RunData;
import com.edgclub.edg.uc.ExitDialog;
import com.edgclub.edg.uc.YueHeiDialog1;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.JsonUtil;
import com.edgclub.edg.utils.util.StringUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.List;

/**
 */
public class GroupAdapter2 extends GzBaseAdapter {
    private List<GroupBean> datas = null;
    private Context context;
    private GlideUtils glideUtils;
    public GroupAdapter2(Context context) {
        this.context = context;
        glideUtils = new GlideUtils();
    }

    //创建新View，被LayoutManager所调用
    @Override
    public GroupAdapter2.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_grouplist2, viewGroup, false);
        ViewHolder vh = new ViewHolder(view, mListener, mLongClickListener);
        return vh;
    }

    @SuppressWarnings("ResourceAsColor")
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final GroupAdapter2.ViewHolder viewHolder = (ViewHolder) holder;
        final GroupBean groupBean = datas.get(position);
        viewHolder.mGroup_name.setText(groupBean.getGname());
        viewHolder.mGroup_introduce.setText(groupBean.getRemark());
        viewHolder.mGroup_peoplenum.setText("本群共有" + groupBean.getMembers() + "人");
        if (!groupBean.isJoin()) {
            viewHolder.mPull_text.setText("加入群组");
            viewHolder.mPull_icon.setVisibility(View.GONE);
            viewHolder.mPull_layout.setBackgroundResource(R.drawable.shape_gz_bq_red_selector);
        } else {
            viewHolder.mPull_text.setText("聊天");
            viewHolder.mPull_icon.setVisibility(View.GONE);
            viewHolder.mPull_layout.setBackgroundResource(R.drawable.shape_gz_bq_red_selector);
        }
        glideUtils.Glidde_loadCropCircleImage(context, groupBean.getLogo(), viewHolder.mUser_icon);
        viewHolder.mPull_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (groupBean.isJoin()) {
                    Intent intent = new Intent(context, ChatActivity.class);
                    intent.putExtra("chatBean", RunData.getChatBeanMode(groupBean, null));
                    context.startActivity(intent);
//                    showDialog(context,groupBean,viewHolder);
                } else {
                    addGroup(groupBean, viewHolder, true);
                }
            }
        });
    }


    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    public List<GroupBean> getDatas() {
        return datas;
    }

    public void setDatas(List<GroupBean> datas) {
        this.datas = datas;
    }

    private void showDialog(Context context, final GroupBean groupBean, final ViewHolder viewHolder) {
        final ExitDialog dialog = new ExitDialog(context);
        dialog.show();
        dialog.setSubmitListener(new ExitDialog.SubmitClickListener() {
            @Override
            public void submitClick() {
                dialog.dismiss();
                addGroup(groupBean, viewHolder, false);
            }
        });
        dialog.setCancelListener(new ExitDialog.CancelClickListener() {
            @Override
            public void cancelClick() {
                dialog.dismiss();
            }
        });
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RViewHolder {
        private ImageView mUser_icon;
        private TextView mGroup_name;
        private TextView mGroup_peoplenum;
        private TextView mGroup_introduce;
        private LinearLayout mPull_layout;
        private TextView mPull_text;
        private ImageView mPull_icon;

        public ViewHolder(View view, MyItemClickListener listener, MyItemLongClickListener longClickListener) {
            super(view, listener, longClickListener);
            mUser_icon = (ImageView) view.findViewById(R.id.user_icon);
            mGroup_name = (TextView) view.findViewById(R.id.group_name);
            mGroup_peoplenum = (TextView) view.findViewById(R.id.group_peoplenum);
            mGroup_introduce = (TextView) view.findViewById(R.id.group_introduce);
            mPull_layout = (LinearLayout) view.findViewById(R.id.pull_layout);
            mPull_text = (TextView) view.findViewById(R.id.pull_text);
            mPull_icon = (ImageView) view.findViewById(R.id.pull_icon);
        }

    }

    private void addGroup(GroupBean groupBean, ViewHolder holder, boolean res) {
        MessageParam messageParam = new MessageParam();
        messageParam.setGroupid(groupBean.getGroupid());
        messageParam.setUserId(new UserPrefUtils(context).getUser().getId());
        if (res)
            new FriendApi(context).AddGroupQuit(messageParam, new ResultClass(groupBean, holder));
        else
            new FriendApi(context).quitGroup(messageParam, new ResultClass(groupBean, holder));
    }

    class ResultClass implements VolleyListener {
        GroupBean groupBean;
        ViewHolder viewHolder;

        public ResultClass(GroupBean groupBean, ViewHolder holder) {
            this.groupBean = groupBean;
            this.viewHolder = holder;
        }

        @Override
        public void onStart(String tag) {
            showDialog(context, "正在请求...", false);
        }

        @Override
        public void onSuccess(String response, String tag) {
            dismissDialog();
            if (JSONHelper.isSuccess(response)) {
                RunData data = JsonUtil.getMode(response,
                        RunData.class);
                if (tag.equals(ApiConfig.APIURL.URL_GROUP_APPLY)) {
                    if ("加入成功".equals(data.getData())) {
                        //统计点击事件
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("gname", groupBean.getGname());
                        MobclickAgent.onEvent(context, "EC_JOIN_GROUP_SUCCESS", map);

                        viewHolder.mPull_text.setText("聊天");
                        groupBean.setJoin(true);
                        viewHolder.mPull_icon.setVisibility(View.GONE);
                        viewHolder.mPull_layout.setBackgroundResource(R.drawable.shape_gz_bq_red_selector);

                        Intent intent = new Intent(context, ChatActivity.class);
                        intent.putExtra("chatBean", RunData.getChatBeanMode(groupBean, null));
                        context.startActivity(intent);

                    } else {
                        StringUtils.postToast(context, data.getData() + "");
                    }

                } else if (tag.equals(ApiConfig.APIURL.URL_GROUP_USERS_QUIT)) {
                    if ("退群成功".equals(data.getData())) {
                        groupBean.setJoin(false);
                        viewHolder.mPull_text.setText("加入群组");
                        viewHolder.mPull_icon.setVisibility(View.GONE);
                        viewHolder.mPull_layout.setBackgroundResource(R.drawable.shape_gz_bq_red_selector);
                        StringUtils.postToast(context, "退群成功");
                        //统计点击事件
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("gname", groupBean.getGname());
                        MobclickAgent.onEvent(context, "EC_QUIT_GROUP_SUCCESS", map);
                    } else {
                        StringUtils.postToast(context, data.getData() + "");
                    }
                }
            } else if (JSONHelper.getResultCode(response) == 7006) {
                StringUtils.postToast(context, JSONHelper.getResultMsg(response) + "");
            } else if (JSONHelper.getResultCode(response) == 7005) {
                YueheiDialog();
            } else {
                StringUtils.postToast(context, JSONHelper.getResultMsg(response));
            }

        }

        @Override
        public void onFaile(VolleyError volleyError, String tag) {
            dismissDialog();
            ResultCode.toastVolleyError(context, volleyError);
        }
    }

    private void YueheiDialog() {
        final YueHeiDialog1 dialog = YueHeiDialog1.create(context, true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        dialog.setSubmitListener(new YueHeiDialog1.SubmitClickListener() {
            @Override
            public void submitClick() {
                dialog.dismiss();
            }
        });
    }

}
